// pages/EmpMessage/EmpChat.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        oid: "",//会话人id
        cuser: {},//客户用户数据
        euser: {},//员工用户数据

        listdata: [],
        page: 1,//页码
        totalpage: 1,//总页数
        msgcontent: "",//发送的消息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        _this.setData({
            oid: options.id
        })
        _this.GetChatDetail(function () {
            _this.GetChatList(function(){
              _this.setData({
                scrollTop: _this.data.listdata.length * 150
              })
            });
        })
    },
    GetChatDetail: function (fn) {
        var _this = this;
        var oid = _this.data.oid;
        app.http.POST('/MiniManagerEmp/GetChatDetail', { oid: oid }, (err, data) => {
            if (data.code == 1) {
                _this.setData({
                    cuser: data.data.ClientData,
                    euser: data.data.EmpUserData,
                });
                var cuser = this.data.cuser;
                var text = cuser.NickName ? cuser.NickName : "私聊消息详情";
                wx.setNavigationBarTitle({
                    title: text,
                })
                fn && fn();
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    GetChatList: function (fn) {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var listdata = _this.data.listdata;
        var page = _this.data.page;
        var oid = _this.data.oid;
        app.http.POST('/MiniManagerEmp/GetChatHistory', { page: page, oid: oid }, (err, data) => {
            if (data.code == 1) {
                var list = data.data.List;
                list = list.reverse();
                if (list != null && list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        list[i].SendTime = app.common.dateFormatType(list[i].SendTime, 2);
                        list[i]["ImgUrl"] = list[i].SendType == 2 ? list[i].EmpImg : _this.data.cuser.HeadImgurl;
                    }
                    listdata = list.concat(listdata);
                    _this.setData({
                        "listdata": listdata,
                        "totalpage": data.data.TotalPage
                    });
                }
                fn&&fn();
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
            wx.hideLoading();
        })
    },
    MsgTextChange: function (e) {
        var content = e.detail.value;
        this.setData({
            msgcontent: content
        });
    },
    Send: function () {
        var _this = this;
        var listdata = _this.data.listdata;
        var nowdate = new Date();
        if (!app.common.validStr(_this.data.msgcontent)){
            return;
        }
        var obj = {
            SendTime: app.common.dateFormatType(nowdate, 2),
            SendContent: _this.data.msgcontent,
            SendType: 2,
            ImgUrl: _this.data.euser.HeadImgurl,
            EmpImg: _this.data.euser.HeadImgurl
        };
        listdata.push(obj);
        this.setData({
            msgcontent: "",
            listdata: listdata,
            scrollTop: listdata.length * 150
        });
        app.http.POST('/MiniManagerEmp/ChatSubmit', {
            sendContent: obj.SendContent,
            oid: _this.data.oid
        }, (err, data) => {
            if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            } else if (data.code != 1) {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    upper:function(e){
      console.log(e);
      var _this = this;
      console.log(_this.data.page++);
      if (_this.data.page <= _this.data.totalpage) {
        _this.setData({
          "page": _this.data.page++,
        })
        _this.GetChatList(); 
      } else {
        return false;
      }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})